package com.share.rabbitmq.direct;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.share.rabbitmq.RabbitMQConnection;

import java.util.HashMap;
import java.util.Map;

/**
 * @desc:直接交换机生产者
 * @author:caifan
 * @date:2020/3/1
 */
public class DirectExchangeProducer {
    public static void main(String[] args) throws Exception {
        Connection connect = RabbitMQConnection.connect();
        Channel channel = connect.createChannel();

//        simple(channel);
        dlxDirect(channel);
    }

    /**
     * 死信测试
     * @param channel
     * @throws Exception
     */
    private static void dlxDirect(Channel channel) throws Exception {
        final String exchange = "my.direct.exchange";
        final String queue = "my.direct.queue";
        final String routingKey = "my.direct.routingKey";
        AMQP.BasicProperties basicProperties = new AMQP.BasicProperties().builder()
                .deliveryMode(2)//持久化
                .expiration("10000")
                .build();
        String message = "{\"name\":\"张三\"}";
        channel.basicPublish(exchange, routingKey, basicProperties, message.getBytes());
    }


    /**
     * 简单使用
     * @param channel
     * @throws Exception
     */
    private static void simple(Channel channel) throws Exception {
        String exchangeName = "my.directExchange3";
        String routingKey = "my.directExchange3.key";

        /*
         * 创建交换机，exchangeDeclare有多个参数的重载方法 exchangeName, exchangeType, durable, autoDelete, args
         */
        channel.exchangeDeclare(exchangeName, BuiltinExchangeType.DIRECT, true);

        channel.queueDeclare(routingKey, false, false, false, null);

        for (int i = 0;i < 10;i++) {
            String messge = "hello message";
            messge = messge + i;
            channel.basicPublish(exchangeName, routingKey, null, messge.getBytes());
        }
    }
}
